package com.css.dzj.dzjfxyj.modules.reply.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjfxyj.common.audit.service.SysAuditService;
import com.css.dzj.dzjfxyj.common.util.Constant;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.modules.reply.entitiy.JgFxyjFeedbackEntity;
import com.css.dzj.dzjfxyj.modules.reply.service.ReplyService;
import com.css.dzj.dzjfxyj.modules.reply.vo.ReplyVo.MainDistributeVo;
import com.css.dzj.dzjfxyj.modules.reply.vo.ReplyVo.ReplyVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 回复
 */
@RestController
@RequestMapping(value = "/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 查看所有待回复信息
     *
     * @return
     */
    @GetMapping(value = "/getAllReply")
    public R getAllReply(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize,
                         HttpServletRequest request) {
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults results = replyService.getAllReply(pageRequest);
        //baseService.logSuccess(3, "查询待回复成功", "2");
        String s = JSON.toJSONString(results);
        sysAuditService.saveSuccessAudit("查看所有待回复信息成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
        return R.ok().put("data", results);
    }

    /**
     * 查看详情回复页面VO
     *
     * @return
     */
    @GetMapping(value = "/getOne")
    public R getOne(String id) {
        ReplyVo replyVo = replyService.queryOne(id);
        //baseService.logSuccess(3, "查询回复成功", "2");
        String s = JSON.toJSONString(replyVo);
        sysAuditService.saveSuccessAudit("查看详情回复页面VO成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", replyVo);
    }

    /**
     * 新增回复信息
     */
    @PostMapping(value = "/addForce")
    public R addForce(@RequestBody ReplyVo vo, HttpServletRequest request) {
        String msg = replyService.addReply(vo, request);
        //baseService.logSuccess(3, "新增回复成功", "2");
        String s = JSON.toJSONString(vo);
        sysAuditService.saveSuccessAudit("新增回复信息成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
        return R.ok();
    }

    /**
     * 条件查询
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam(name = "pageSize", required = false) Integer pageSize,
                     @RequestParam(name = "pageNo", required = false) Integer pageNo,
                     @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                     @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                     @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                     @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                     @RequestParam(name = "startTime", required = false) String startTime,
                     @RequestParam(name = "endTime", required = false) String endTime,
                     @RequestParam(name = "startTime1", required = false) String startTime1,
                     @RequestParam(name = "endTime1", required = false) String endTime1) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<MainDistributeVo> page = replyService.getPage(pageRequest, fxyjDomainId, fxyjTitle, fxyjLevel,
                fxyjObjectName, startTime, endTime, startTime1, endTime1);
        //baseService.logSuccess(3, "查询待回复成功", "2");
        String s = JSON.toJSONString(page);
        sysAuditService.saveSuccessAudit("条件查询成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
        return R.ok().put("data", page);
    }

    /**
     * 修改回复信息状态
     */
    @PostMapping(value = "/updForce")
    public R updForce(@RequestBody JgFxyjFeedbackEntity jgFxyjFeedbackEntity) {
        //String msg = replyService.addReply(jgFxyjFeedbackEntity);
        return R.ok();
    }

    /**
     * 点击回复按钮时回显数据
     *
     * @param id
     * @return
     */
    @PostMapping("/queryAcquire")
    public R queryAcquire(String id, String from, HttpServletRequest request) {
        ReplyVo replyVo = replyService.queryAcquire(id, from, request);
        //baseService.logSuccess(3, "查询回复回显成功", "2");
        String s = JSON.toJSONString(replyVo);
        sysAuditService.saveSuccessAudit("点击回复按钮时回显数据成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", replyVo);
    }

    /**
     * 通过主表ID获取所有回复信息
     *
     * @param id
     * @return
     */
    @GetMapping("/queryList")
    public R queryList(String id) {
        List<JgFxyjFeedbackEntity> list = replyService.queryList(id);
        //baseService.logSuccess(3, "查询所有回复成功", "2");
        String s = JSON.toJSONString(list);
        sysAuditService.saveSuccessAudit("通过主表ID获取所有回复信息成功", Constant.AUDIT_MODULE_REPLY, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", list);
    }

}
